<?php

/**

 * 我的订单

 *

 *

 *

 * * @网店运维 (c) 2015-2018  Inc. (http://www.shopwwi.com)
 * @license    http://www.shopwwi.c om
 * @link       交流群号：111731672
 * @since      网店运维提供技术支持 授权请购买shopnc授权

 */



//use Shopnc\Tpl;



defined('InShopNC') or exit('Access Invalid!');



class member_ordersControl extends mobileHomeControl {



    public function __construct(){

        parent::__construct();

    }


    public function indexOp(){
        $model_order = Model('order');

        $condition = array();
        $condition['buyer_id'] = $_SESSION['member_id'];

        $order_list_array = $model_order->getOrderList($condition, $this->page, '*', 'order_id desc','', array('order_goods'));

        $token = md5($_SESSION['member_name'] . strval(TIMESTAMP) . strval(rand(0,999999)));

        $order_group_list = array();
        $order_pay_sn_array = array();
        foreach ($order_list_array as $value) {
            //显示取消订单
            $value['if_cancel'] = $model_order->getOrderOperateState('buyer_cancel',$value);
            //显示收货
            $value['if_receive'] = $model_order->getOrderOperateState('receive',$value);
            //显示锁定中
            $value['if_lock'] = $model_order->getOrderOperateState('lock',$value);
            //显示物流跟踪
            $value['if_deliver'] = $model_order->getOrderOperateState('deliver',$value);

            $order_group_list[$value['pay_sn']]['order_list'][] = $value;

            //如果有在线支付且未付款的订单则显示合并付款链接
            if ($value['order_state'] == ORDER_STATE_NEW) {
                $order_group_list[$value['pay_sn']]['pay_amount'] += $value['order_amount'];
            }
            $order_group_list[$value['pay_sn']]['add_time'] = $value['add_time'];

            //记录一下pay_sn，后面需要查询支付单表
            $order_pay_sn_array[] = $value['pay_sn'];
        }

        $new_order_group_list = array();
        foreach ($order_group_list as $key => $value) {
            $value['pay_sn'] = strval($key);
            $new_order_group_list[] = $value;
        }

        $page_content = $model_order->showpage();
        Tpl::output('order_group_list',$new_order_group_list);
        Tpl::output('page',$page_content);
        Tpl::showpage('member/member_order');
    }

    /**
     * 用户中心右边，小导航
     *
     * @param string    $menu_type  导航类型
     * @param string    $menu_key   当前导航的menu_key
     * @return
     */
    private function profile_menu($menu_key='') {
        Language::read('member_layout');
        $menu_array = array(
            array('menu_key'=>'member_order','menu_name'=>Language::get('nc_member_path_order_list'), 'menu_url'=>'index.php?act=member_order'),
            array('menu_key'=>'member_order_recycle','menu_name'=>'回收站', 'menu_url'=>'index.php?act=member_order&recycle=1'),
        );
        Tpl::output('member_menu',$menu_array);
        Tpl::output('menu_key',$menu_key);
    }
    /**

     * 订单列表

     */

    public function order_listOp() {

        $model_order = Model('order');



        $condition = array();

        $condition['buyer_id'] = $_SESSION['member_id'];

        $condition['order_type'] = array('in',array(1,3));

        $order_list_array = $model_order->getNormalOrderList($condition, $this->page, '*', 'order_id desc','', array('order_goods'));



        $order_group_list = array();

        $order_pay_sn_array = array();

        foreach ($order_list_array as $value) {

            //显示取消订单

            $value['if_cancel'] = $model_order->getOrderOperateState('buyer_cancel',$value);

            //显示收货

            $value['if_receive'] = $model_order->getOrderOperateState('receive',$value);

            //显示锁定中

            $value['if_lock'] = $model_order->getOrderOperateState('lock',$value);

            //显示物流跟踪

            $value['if_deliver'] = $model_order->getOrderOperateState('deliver',$value);



            //商品图

            foreach ($value['extend_order_goods'] as $k => $goods_info) {

                $value['extend_order_goods'][$k]['goods_image_url'] = cthumb($goods_info['goods_image'], 240, $value['store_id']);

            }



            $order_group_list[$value['pay_sn']]['order_list'][] = $value;



            //如果有在线支付且未付款的订单则显示合并付款链接

            if ($value['order_state'] == ORDER_STATE_NEW) {

                $order_group_list[$value['pay_sn']]['pay_amount'] += $value['order_amount'] - $value['rcb_amount'] - $value['pd_amount'];

            }

            $order_group_list[$value['pay_sn']]['add_time'] = $value['add_time'];



            //记录一下pay_sn，后面需要查询支付单表

            $order_pay_sn_array[] = $value['pay_sn'];

        }



        $new_order_group_list = array();

        foreach ($order_group_list as $key => $value) {

            $value['pay_sn'] = strval($key);

            $new_order_group_list[] = $value;

        }



        $page_count = $model_order->gettotalpage();



        output_data(array('order_group_list' => $new_order_group_list), mobile_page($page_count));
//        Tpl::output('home_member_info',$member_info);
        Tpl::showpage('member/member_order');

    }



    /**

     * 取消订单

     */

    public function order_cancelOp() {

        $model_order = Model('order');

        $logic_order = Logic('order');

        $order_id = intval($_POST['order_id']);



        $condition = array();

        $condition['order_id'] = $order_id;

        $condition['buyer_id'] = $_SESSION['member_id'];

        $condition['order_type'] = 1;

        $order_info = $model_order->getOrderInfo($condition);

        $if_allow = $model_order->getOrderOperateState('buyer_cancel',$order_info);

        if (!$if_allow) {

            output_error('无权操作');

        }

        if (TIMESTAMP - 86400 < $order_info['api_pay_time']) {

            $_hour = ceil(($order_info['api_pay_time']+86400-TIMESTAMP)/3600);

            output_error('该订单曾尝试使用第三方支付平台支付，须在'.$_hour.'小时以后才可取消');

        }

        $result = $logic_order->changeOrderStateCancel($order_info,'buyer', $_SESSION['member_name'], '其它原因');

        if(!$result['state']) {

            output_error($result['msg']);

        } else {

            output_data('1');

        }

    }



    /**

     * 订单确认收货

     */

    public function order_receiveOp() {

        $model_order = Model('order');

        $logic_order = Logic('order');

        $order_id = intval($_POST['order_id']);



        $condition = array();

        $condition['order_id'] = $order_id;

        $condition['buyer_id'] = $_SESSION['member_id'];

        $condition['order_type'] = 1;

        $order_info = $model_order->getOrderInfo($condition);

        $if_allow = $model_order->getOrderOperateState('receive',$order_info);

        if (!$if_allow) {

            output_error('无权操作');

        }



        $result = $logic_order->changeOrderStateReceive($order_info,'buyer', $_SESSION['member_name'],'签收了货物');

        if(!$result['state']) {

            output_error($result['msg']);

        } else {

            output_data('1');

        }

    }



    /**

     * 物流跟踪

     */

    public function search_deliverOp(){

        $order_id   = intval($_POST['order_id']);

        if ($order_id <= 0) {

            output_error('订单不存在');

        }



        $model_order    = Model('order');

        $condition['order_id'] = $order_id;

        $condition['buyer_id'] = $_SESSION['member_id'];

        $order_info = $model_order->getOrderInfo($condition,array('order_common','order_goods'));

        if (empty($order_info) || !in_array($order_info['order_state'],array(ORDER_STATE_SEND,ORDER_STATE_SUCCESS))) {

            output_error('订单不存在');

        }



        $express = rkcache('express',true);

        $e_code = $express[$order_info['extend_order_common']['shipping_express_id']]['e_code'];

        $e_name = $express[$order_info['extend_order_common']['shipping_express_id']]['e_name'];



        $deliver_info = $this->_get_express($e_code, $order_info['shipping_code']);

        output_data(array('express_name' => $e_name, 'shipping_code' => $order_info['shipping_code'], 'deliver_info' => $deliver_info));

    }



    /**

     * 从第三方取快递信息

     *

     */

    public function _get_express($e_code, $shipping_code){





        $content = Model('express')->get_express(e_code, $shipping_code);

        

        if (empty($content)) {

            output_error('物流信息查询失败');

        }

        $output = array();

        foreach ($content as $k=>$v) {

            if ($v['time'] == '') continue;

            $output[]= $v['time'].'&nbsp;&nbsp;'.$v['context'];

        }



        return $output;

    }



}

